import {Component, OnInit, ViewChild} from '@angular/core';
import {HttpService} from "../../../lib/service/http.service";
import {adSystemApiUrl, tuikeSystemUrl} from "../../../../config/api";
import {ModalDirective} from "ngx-bootstrap";
import {Headers, RequestOptions} from "@angular/http";
import {LoginService} from "../../../lib/service/tuike-login.service";


export class AddInvitation {
  number: string
}

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {

  addInvitation = new AddInvitation;
  searchData = '';
  invitationList = [];
  userName: string;
  psd: string;
  password_again: string;
  operateId: number;  // 当前正在操作的账号ID
  resetPsd: string;
  resetPsdAgain: string;
  status = '';

  type: string;
  message: string;
  isShow = true;
  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 20;
  _isSpinning = false;
  error = false;
  addStatus = false;
  canVisit = false;

  @ViewChild('addClientModal') public addClientModal: ModalDirective;
  @ViewChild('resetPsdModal') public resetPsdModal: ModalDirective;
  @ViewChild('addModal') public addModal: ModalDirective;


  constructor(private httpService: HttpService, private loginService: LoginService) {
  }

  ngOnInit() {
    this.canVisit = this.loginService.canVisitCheck('invitation_add');
    this.getInvitationList(1);
  }

  keyDown(e) {
    e.code === 'Enter' ? this.search() : '';
  }

  //重置
  reset() {
    this.searchData = '';
    this.getInvitationList(1);
  }

  //搜索
  search() {
    this.getInvitationList(1);
  }

  showAddModal() {
    this.addModal.show();
  }

  checkStatus(e) {
    if(e === null) {
      this.error = true;
    }else if (!/^([1-9]\d|\d)$/.test(e) || e == '0') {
      console.log(3222);
      this.error = false;
    } else {
      this.error = true;
    }
  }

  add() {
    let url = `${tuikeSystemUrl}/api/v1/admin/invite_code/create`;
    if(this.error) {
      this.addStatus = true;
      const headers = new Headers({'Content-Type': 'application/json'});
      const options = new RequestOptions({headers: headers});
      const body = JSON.stringify({'count': this.addInvitation.number});
      return this.httpService.httpPostAuth(url, body, options).subscribe(
        res => {
          console.log(res);
          if (res.code == 200) {
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.addStatus = false;
            this.addModal.hide();
            this.currentPage = 1;
            setTimeout(() => {
              this.getInvitationList(1);
            },500);
            this.addInvitation.number = '';
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
            this.addStatus = false;
          }
        }
      );
    }
  }

  cancel() {
    this.addModal.hide();
    this.addInvitation.number = '';
  }


  // 获取客户列表

  getInvitationList(page: number) {
    this._isSpinning = true;
    let searchType = '';
    if (this.searchData.length === 28) {
      searchType = 'tuike_openid';
    } else {
      searchType = 'code';
    }
    this.currentPage = 1;
    let url = `${tuikeSystemUrl}/api/v1/admin/invite_code/code_list?page=${page}&pageSize=${this.itemsPerPage}&${searchType}=${this.searchData}&status=${this.status}`;
    return this.httpService.httpGetAuth(url).subscribe(res => {
      if (res.code == 200) {
        this._isSpinning = false;
        this.invitationList = res.data.data;
        this.totalItems = res.data.total;
      }
    })
  }


  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getInvitationList(event.page);
  }
}
